//
//  KnowledgeView.m
//  CookGuest
//
//  Created by dllo on 15/9/11.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "KnowledgeView.h"

@implementation KnowledgeView

-(id)initWithFrame:(CGRect)frame
{
    self.buttonArray = [[NSMutableArray alloc]init];
    self.buttonTitleArray = @[@"精选",@"健康养生",@"烹饪技巧",@"美食精选",@"美食杂谈"];
    
    self = [super initWithFrame:frame];
    if (self) {
        
        for (int i = 0; i < self.buttonTitleArray.count; i ++) {
            
            self.button = [UIButton buttonWithType:UIButtonTypeCustom];
            self.button.frame = CGRectMake(i * SCREEN_WIDTH / 5, 0, SCREEN_WIDTH / 5, SCREEN_HEIGHT / 14);
            self.button.backgroundColor = [UIColor whiteColor];
            
            
            [self.button.titleLabel setFont:[UIFont systemFontOfSize:15]];
            self.button.tag = i;
            [self.button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
            [self.button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [self.button setTitle:[self.buttonTitleArray objectAtIndex:i] forState:UIControlStateNormal];
            [self.buttonArray addObject:self.button];
            [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:self.button];
            
            
        }
        
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 14 - 2 , SCREEN_WIDTH / 5, 2)];
        self.label.backgroundColor = [UIColor redColor];
        [self addSubview:self.label];
        
        
        
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 14 , SCREEN_WIDTH, self.frame.size.height  - SCREEN_HEIGHT / 14 - 64)];
        self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.buttonTitleArray.count, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
        [self addSubview:self.scrollView];
    }
    return self;
}




-(void)click:(UIButton *)sender
{
    
    for (UIButton * but in self.buttonArray) {
        if (sender == but) {
            
            
            but.selected = YES;
            CGFloat xlen = (sender.tag) *  SCREEN_WIDTH;
            [self.scrollView  setContentOffset:CGPointMake(xlen, 0) ];
            
            self.label.frame = CGRectMake((sender.tag) * SCREEN_WIDTH / 5 , SCREEN_HEIGHT / 14 - 2 , SCREEN_WIDTH / 5, 2);
        }
        
        else
        {
            but.selected = NO;
        }
        
    }
    
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    self.label.frame = CGRectMake(SCREEN_WIDTH / 5 * page , SCREEN_HEIGHT / 14 - 2, SCREEN_WIDTH / 5, 2);
    
    for (UIButton * btn in self.buttonArray) {
        if (page == btn.tag ) {
            
            btn.titleLabel.textColor = [UIColor redColor];
            
            
            
            
        } else {
            btn.titleLabel.textColor = [UIColor darkGrayColor];
        }
        
        
    }
    
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
